#include <stdio.h>
#include "ohos_init.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_adc.h"

/**
 * @brief Reads a piece of sampled data from a specified ADC channel based on the input parameters.
 *
 *
 *
 * @param channel Indicates the ADC channel index.
 * @param data Indicates the pointer to the address for storing the read data.
 * @param equModel Indicates the equation model.
 * @param curBais Indicates the analog power control mode.
 * @param rstCnt Indicates the count of the time from reset to conversion start.
 *               One count is equal to 334 ns. The value must range from 0 to 0xFF0.
 * @return Returns {@link IOT_SUCCESS} if the PWM signal output is stopped;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 1.0
 * @version 1.0
 */
//unsigned int IoTAdcRead(unsigned int channel, unsigned short *data, IotAdcEquModelSel equModel,
//                     IotAdcCurBais curBais, unsigned short rstCnt);

#define ADC_PIN 13
#define ADC_IDX 6

void adc_entry(){
    printf("pwm_entry called \n");
    IoTGpioInit(ADC_PIN);
    IoTGpioSetDir(ADC_PIN,IOT_GPIO_DIR_IN);
    IoTGpioSetFunc(ADC_PIN,0);
    while(1){
        unsigned short data = 0;
        int ret = IoTAdcRead(ADC_IDX,&data,IOT_ADC_EQU_MODEL_1,IOT_ADC_CUR_BAIS_DEFAULT, 0); 
        if(ret == IOT_SUCCESS){
            printf("read %d\n",(int)data);
        }else{
            printf("read error\n");
        }
        usleep(1000*1000);
    }

}

APP_FEATURE_INIT(adc_entry);

